package com.gaga.controller;

import com.gaga.pojo.SysUser;
import com.gaga.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
class DemoController {

    @Resource
    private UserService userService;

    /*第一个 Thymeleaf 测试*/
    @GetMapping(value = "/hello/{id}")
    public String getUser(@PathVariable("id") Long usrId, Model model) {
        SysUser user = userService.getUser(usrId);
        System.out.println(user);
        model.addAttribute("user", user);
        return "demo/hello";
    }

    /* Thymeleaf 变量表达式测试 */
    @GetMapping(value = "/string")
    public String getString(Model model) {//演示赋值、字符串拼接
        System.out.println("gaga");
        model.addAttribute("userName", "SuperGaGa");
        return "demo/string";
    }

    /* 内联语法测试 */
    @GetMapping(value = "/inline")
    public String inline(Model model) {//演示内联
        model.addAttribute("userName", "SuperGaGa");
        return "demo/inline";
    }

    /* Thymeleaf 基本对象测试 */
    @GetMapping(value = "/entity")
    public String getString(Model model, HttpServletRequest request) {//演示赋值、字符串拼接
        model.addAttribute("userName", "SuperGaGa");
        request.setAttribute("test", "request");
        request.getSession().setAttribute("test", "session");
        model.addAttribute("date", new Date());
        return "demo/entity";
    }

    /* 条件判断测试 */
    @GetMapping(value = "/if")
    public String ifunless(Model model) {//演示条件判断 if/unless
        model.addAttribute("flag", "yes");
        return "demo/if";
    }

    /* switch 选择测试 */
    @GetMapping(value = "/switch")
    public String switchTest(Model model) {//演示条件判断 if/unless
        model.addAttribute("sex", "*");
        return "demo/switch";
    }

    /* for循环测试 */
    @GetMapping(value = "/for")
    public String list(Model model) {//演示 for 循环
        List<SysUser> list = userService.findAllUsers();
        model.addAttribute("users", list);
        return "demo/for";
    }

    /*引入页面信息测试*/
    @GetMapping(value = "/index")
    public String index() {
        return "demo/demoTwo/index";
    }

    /*框架模式页面测试*/
    @GetMapping(value = "/fragment")
    public String layout() {
        return "demo/demoTwo/fragment";
    }

    /*替换页面内容测试*/
    @GetMapping(value = "/test")
    public String main() {
        return "demo/demoTwo/test";
    }


//    @RequestMapping(value = "/login")
//    public String login(String usrName, String usrPassword, Model model, HttpSession session){
//        System.out.println("login " + usrName + " " + usrPassword);
//        SysUser user = userService.login(usrName,usrPassword);
//        System.out.println(user);
//        if(user!=null){
//            session.setAttribute("loginUser",user);
//            return "redirect:/main";
//        }else{
//            model.addAttribute("msg","用户名或密码错误，登录失败！");
//            return "forward:/login.jsp";
//        }
//    }
//
//    @RequestMapping(value = "/main")
//    public String main(){
//        return "main";
//    }

}
